package steven.sdc.appPoker.Fragment;
import java.util.ArrayList;
import android.view.View.OnClickListener;

import steven.sdc.appPoker.Adapter.AdapterBangDiem;
import steven.sdc.appPoker.Items.ItemBangDiem;

import com.amurani.slidingmenu.R;

import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ListView;
import android.widget.Toast;

public class FragmentBangDiem extends Fragment{
	CheckBox cb_thangtrang;
	ListView lv_bangdiem;
	ArrayList<ItemBangDiem> item=null;
	AdapterBangDiem adapter;
	Button btn_khac;
	final CharSequence[]diaglog = {"Sửa","Xóa","Hủy"};
	final CharSequence[]khac = {"Tổng kết","Cấu hình","Hủy trò chơi"};
	final CharSequence co = "Có";
	final CharSequence khong = "Không";
	private static int id_selected;
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreateView(inflater, container, savedInstanceState);
		View view=inflater.inflate(R.layout.bang_diem, null);
		Init(view);
		
		return view;
	}
	
	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onActivityCreated(savedInstanceState);
		List();
		
		ListViewEvent();
		CheckBoxEvent();
		
	}
	public void CheckBoxEvent()
	{
		cb_thangtrang.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
			
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				// TODO Auto-generated method stub
				if(isChecked==true)
				{
					FragmentThangTrang dt=new FragmentThangTrang();
					getFragmentManager().beginTransaction().add(R.id.fragment_content, dt,null).addToBackStack(null).commitAllowingStateLoss();
				}
				
			}
		}); 
	
	}
	public void ListViewEvent()
	{
		lv_bangdiem.setOnItemClickListener(new AdapterView.OnItemClickListener() {

			public void onItemClick(AdapterView<?> arg0, View arg1,  final int arg2,
					long arg3) {
				
				AlertDialog.Builder alert= new AlertDialog.Builder(getActivity());
				alert.setTitle("Chọn thao tác");
				alert.setItems(diaglog, new DialogInterface.OnClickListener() {
					
					public void onClick(DialogInterface dialog, int which) {
						// TODO Auto-generated method stub
						if(which == 0){
							
						}
						if(which == 1){
							AlertDialog.Builder ok = new AlertDialog.Builder(getActivity());
							ok.setTitle("Bạn có muốn xóa?");
							ok.setPositiveButton(co, new DialogInterface.OnClickListener() {
								
								public void onClick(DialogInterface dialog, int which) {
									// TODO Auto-generated method stub
									
										item.remove(arg2);
										adapter.notifyDataSetChanged();
										Toast.makeText(getActivity(), "Xóa thành công!!!", Toast.LENGTH_SHORT).show();
									
								}
							});
							ok.setNegativeButton(khong, new DialogInterface.OnClickListener() {
								
								public void onClick(DialogInterface dialog, int which) {
									// TODO Auto-generated method stub
									
								}
							});
							ok.show();
//							item.remove(arg2);
//							adapter.notifyDataSetChanged();
//							Toast.makeText(getActivity(), "Xóa thành công!!!", Toast.LENGTH_SHORT).show();
						}
						if(which == 2){
	
						}
					}
				});
				//AlertDialog.Builder alert = new AlertDialog.Builder(FragmentBangDiem.class);
				
				alert.show();	 
			}
		}) ;
		
	}
public void List()
{
	ItemBangDiem dt=new ItemBangDiem(1, 2, 8, 9, 7);
	ItemBangDiem dt1=new ItemBangDiem(2, 2, 23, 1, 7);
	ItemBangDiem dt2=new ItemBangDiem(1, 2, 8, 9, 7);
	ItemBangDiem dt3=new ItemBangDiem(2, 2, 23, 1, 7);
	ItemBangDiem dt4=new ItemBangDiem(1, 2, 8, 9, 7);
	ItemBangDiem dt5=new ItemBangDiem(2, 2, 23, 1, 7);
	ItemBangDiem dt6=new ItemBangDiem(2, 2, 23, 1, 6);
	ItemBangDiem dt7=new ItemBangDiem(1, 2, 8, 9, 7);
	ItemBangDiem dt8=new ItemBangDiem(2, 2, 23, 1, 7);
	ItemBangDiem dt9=new ItemBangDiem(1, 2, 8, 9, 7);
	ItemBangDiem dt10=new ItemBangDiem(2, 2, 23, 1, 7);
	ItemBangDiem dt12=new ItemBangDiem(1, 2, 8, 9, 7);
	ItemBangDiem dt13=new ItemBangDiem(2, 2, 23, 1, 7);
	ItemBangDiem dt14=new ItemBangDiem(1, 2, 8, 9, 7);
	ItemBangDiem dt15=new ItemBangDiem(2, 2, 23, 1, 7);
	ItemBangDiem dt16=new ItemBangDiem(2, 2, 23, 1, 6);
	ItemBangDiem dt17=new ItemBangDiem(1, 2, 8, 9, 7);
	ItemBangDiem dt18=new ItemBangDiem(2, 2, 23, 1, 7);
	item.add(dt);
	item.add(dt1);
	item.add(dt2);
	item.add(dt3);
	item.add(dt4);
	item.add(dt5);
	item.add(dt8);
	item.add(dt7);
	item.add(dt6);
	item.add(dt8);
	item.add(dt9);
	item.add(dt10);
	item.add(dt12);
	item.add(dt13);
	item.add(dt14);
	item.add(dt15);
	item.add(dt18);
	item.add(dt17);
	item.add(dt16);
	item.add(dt18);
	
	adapter.notifyDataSetChanged();
	
}

	public void Init(View view)
	{	cb_thangtrang=(CheckBox)view.findViewById(R.id.checkBox_thangtrang);
		lv_bangdiem=(ListView)view.findViewById(R.id.listView_bangdiem);
		btn_khac = (Button) view.findViewById(R.id.btn_khac);
btn_khac.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				// TODO Auto-generated method stub
				AlertDialog.Builder alertkhac = new AlertDialog.Builder(getActivity());
				alertkhac.setTitle("Chọn thao tác");
				alertkhac.setItems(khac, new DialogInterface.OnClickListener() {
					
					public void onClick(DialogInterface dialog, int which) {
						// TODO Auto-generated method stub
						if(which == 0){
							AlertDialog.Builder ok = new AlertDialog.Builder(getActivity());
							ok.setTitle("Bạn muốn tổng kết trận đấu?");
							ok.setPositiveButton(co, new DialogInterface.OnClickListener() {
								
								public void onClick(DialogInterface dialog, int which) {
									// TODO Auto-generated method stub
									
									FragmentKetQua frag_ketqua = new FragmentKetQua();
									getFragmentManager().beginTransaction().replace(R.id.fragment_content,frag_ketqua, null).commitAllowingStateLoss();
									
								}
							});
							ok.setNegativeButton(khong, new DialogInterface.OnClickListener() {
								
								public void onClick(DialogInterface dialog, int which) {
									// TODO Auto-generated method stub
									
								}
							});
							ok.show();
							
						}
						if(which == 1){
							
							AlertDialog.Builder ok = new AlertDialog.Builder(getActivity());
							ok.setTitle("Bạn muốn cấu hình lại trận đấu?");
							
							ok.setPositiveButton(co, new DialogInterface.OnClickListener() {
								
								public void onClick(DialogInterface dialog, int which) {
									// TODO Auto-generated method stub
									
									FragmentCauHinh frag_cauhinh = new FragmentCauHinh();
									getFragmentManager().beginTransaction().add(R.id.fragment_content, frag_cauhinh,null).addToBackStack(null).commitAllowingStateLoss();
									
								}
							});
							ok.setNegativeButton(khong, new DialogInterface.OnClickListener() {
								
								public void onClick(DialogInterface dialog, int which) {
									// TODO Auto-generated method stub
									
								}
							});
							ok.show();
						}
					}
				});
				alertkhac.show();
			}
		});
		item =new ArrayList<ItemBangDiem>();
		adapter=new AdapterBangDiem(getActivity(), R.layout.list_item_bangdiem, item);
		lv_bangdiem.setAdapter(adapter);
	}
}
